package soundwave.com.demo.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import soundwave.com.demo.R;
import soundwave.com.demo.entry.LoginSingle;
import soundwave.com.demo.list.LoginList;


/**
 * Created by xu on 2017/4/5.
 */

public class Register extends Activity {
    private ImageView headimg;
    private EditText phone;
    private Button send_code;
    private EditText code;
    private EditText word;
    private TextView text;
    private TextView login;
    private TextView back;
    private Button register;
    private String phone_number, code_number, password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        initView();
        final LoginSingle logsinle = LoginSingle.getInstance();
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                phone_number = phone.getText().toString();
                code_number = code.getText().toString();
                password = word.getText().toString();
                if (phone_number.length() < 11) {
                    Toast.makeText(Register.this, "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
                } else if (phone_number.equals("")) {
                    Toast.makeText(Register.this, "手机号码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    if (code_number.equals("")) {
                        Toast.makeText(Register.this, "验证码不能为空", Toast.LENGTH_SHORT).show();
                    } else {
                        if (password.length() < 6) {
                            Toast.makeText(Register.this, "密码不能少于6位", Toast.LENGTH_SHORT).show();
                        } else {
                            SharedPreferences shared = getSharedPreferences("login", MODE_APPEND);
                            SharedPreferences.Editor editor = shared.edit();
                            editor.putBoolean("isLogin", true);
                            editor.putString("isName", "已登录");
                            editor.putString("Phone", phone_number);
                            editor.commit();
                            logsinle.setLogin(true);
                            logsinle.setName("已登录");
                            logsinle.setPhone(phone_number);

                            Intent intent = new Intent();
                            intent.putExtra("username", "test");
                            setResult(1, intent);
                            finish();
                        }
                    }
                }
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        send_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                phone_number = phone.getText().toString();
                if (phone_number.length() < 11) {
                    Toast.makeText(Register.this, "手机号码必须为11位", Toast.LENGTH_SHORT).show();
                } else {
                    if (phone_number.equals("")) {
                        Toast.makeText(Register.this, "手机号码不能为空", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(Register.this, "发送成功", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initView() {
        back = (TextView) findViewById(R.id.back);
        headimg = (ImageView) findViewById(R.id.headimg);
        phone = (EditText) findViewById(R.id.phone);
        send_code = (Button) findViewById(R.id.send_code);
        code = (EditText) findViewById(R.id.code);
        word = (EditText) findViewById(R.id.word);
        text = (TextView) findViewById(R.id.text);
        login = (TextView) findViewById(R.id.login);
        register = (Button) findViewById(R.id.register);


    }


}
